package com.security.door.controller.attachManage;

import com.github.pagehelper.PageHelper;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.keyAttach;
import com.security.door.model.dto.keyAttachDto;
import com.security.door.service.Impl.keyAttachService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - keyAttachController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class keyAttachController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    keyAttachService ikeyAttachService;

    @GetMapping("/attach/Add")
    public Wrapper InsertAttach(@RequestBody keyAttach entity) {
        if ("".equals(entity.getGuid())) {
            entity.setGuid(UUID.randomUUID().toString());
            entity.setCreatetime(new Date());
            ikeyAttachService.save(entity);
        } else {
            ikeyAttachService.update(entity);
        }
        return WrapMapper.ok(entity);
    }

    @GetMapping(value = "/Attach/queryListWithPage")
    public Wrapper AttachList(keyAttachDto entityDto) {
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        PageHelper.startPage(pageIndex, pageSize);
        var list = ikeyAttachService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping(value = "/Attach/Del/{guid}")
    public Wrapper DeleteOne(@PathVariable("guid") String guid) {
        int result = ikeyAttachService.deleteByKey(guid);
        return WrapMapper.ok(result);
    }

    @GetMapping(value = "/Attach/Status/{guid}")
    public Wrapper UpateStatus(@PathVariable("guid") String guid) {
        int result = ikeyAttachService.updateStatus(guid);
        return WrapMapper.ok(result);
    }
}
